<?php

namespace app\admin\model;

use think\Model;

class Goods extends Model {

    // 表名
    protected $name = 'goods';
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    // 追加属性
    protected $append = [
        'status_text',
        'is_deleted_text',
    ];

    public function getStatusList() {
        return ['0' => __('Status 0'), '1' => __('Status 1')];
    }

    public function getIsDeletedList() {
        return ['0' => __('Is_deleted 0'), '1' => __('Is_deleted 1')];
    }

    public static function getCateList() {
        $cateList = db('goods_cate')->order('sort')->select();
        $list = [];
        foreach ($cateList as $k => &$v) {
            $list[$v['cate_id']] = __($v['cate_title']);
        }

        return $list;
    }

    public static function getBrandList() {
        $brandList = db('goods_brand')->order('sort')->select();
        $list = [];
        foreach ($brandList as $k => &$v) {
            $list[$v['brand_id']] = __($v['brand_title']);
        }

        return $list;
    }

    public function getCateTextAttr($value, $data) {
        $value = $value ? $value : $data['cate_id'];
        $list = $this->getCateList();
        return isset($list[$value]) ? $list[$value] : '';
    }
    
    public function getBrandTextAttr($value, $data) {
        $value = $value ? $value : $data['brand_id'];
        $list = $this->getBrandList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getStatusTextAttr($value, $data) {
        $value = $value ? $value : $data['status'];
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getIsDeletedTextAttr($value, $data) {
        $value = $value ? $value : $data['is_deleted'];
        $list = $this->getIsDeletedList();
        return isset($list[$value]) ? $list[$value] : '';
    }

}
